
package io.hx.modules.sys.controller;


import io.hx.common.annotation.SysLog;
import io.hx.common.utils.PageUtils;
import io.hx.common.utils.R;
import io.hx.common.validator.Assert;
import io.hx.common.validator.ValidatorUtils;
import io.hx.common.validator.group.AddGroup;
import io.hx.common.validator.group.UpdateGroup;
import io.hx.modules.bo.DataCollection;
import io.hx.modules.hos.service.AppointmentService;
import io.hx.modules.patient.service.PatientUserService;
import io.hx.modules.rep.service.DiagnosticService;
import io.hx.modules.sys.entity.SysDeptEntity;
import io.hx.modules.sys.entity.SysUserEntity;
import io.hx.modules.sys.service.SysDeptService;
import io.hx.modules.sys.service.SysUserRoleService;
import io.hx.modules.sys.service.SysUserService;
import io.hx.modules.sys.shiro.ShiroUtils;
import org.apache.commons.lang.ArrayUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.util.*;

/**
 * 系统用户
 *
 * @author Mark sunlightcs@gmail.com
 */
@RestController
@RequestMapping("/sys/user")
public class SysUserController extends AbstractController {
    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private SysUserRoleService sysUserRoleService;
    @Autowired
    private DiagnosticService diagnosticService;
    @Autowired
    private PatientUserService patientUserService;
    @Autowired
    private AppointmentService appointmentService;
    @Autowired
    private SysDeptService deptService;
    /**
     * 所有用户列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("sys:user:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = sysUserService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 获取工作台上的显示信息
     */
    @RequestMapping("/allInfo")
    public R allInfo(HttpSession session) {
        //总价格;
        BigDecimal totalMoney = diagnosticService.queryMoneyAll();
        //用户总数
       Integer  userAll= patientUserService.queryUserAll();
       //接待患者数(查询预约表中状态不为0的数量)
        Integer patientNum=appointmentService.queryPatientAll();
        //复诊人数的查询
        List<Integer> furtherList=appointmentService.queryFurtherAll();
        Integer furtherNum=furtherList.size();
        //初诊人数查询
        List<Integer> firstList=appointmentService.queryFirstAll();
        Integer firstNum=firstList.size();
        //看诊数量查询按月份分组
        List<Integer> visitsNum=appointmentService.queryVisitsAll();
        //就诊金额查询按月份分组
        List<Integer> AmountPrice=diagnosticService.queryAmountAll();
        //看诊数量查询同个不同年龄段分组
        List<Integer> ages=appointmentService.queryAgeAll();
        //看诊数量查询,按照性别进行统计
        List<Integer> genderNum=appointmentService.queryGenderAll();
        DataCollection dataCollection = new DataCollection(totalMoney,userAll,patientNum,furtherNum,firstNum,visitsNum,AmountPrice,ages,genderNum,"",0);
        System.out.println(dataCollection.toString());
        return R.ok().put("totalData3", dataCollection);
    }

    /**
     * 获取登录的用户信息
     */
    @RequestMapping("/info")
    public R info(HttpSession session) {
        System.out.println(getUser().toString());
        session.setAttribute("user",getUser());
        return R.ok().put("user", getUser());
    }
    @RequestMapping("/selectOne")
    public R selectOne(HttpSession session){
       SysUserEntity sysUser =(SysUserEntity)session.getAttribute("user");
       SysDeptEntity deptEntity =deptService.queryByDeptId(sysUser.getDeptId());
       sysUser.setDeptName(deptEntity.getName());
        System.out.println(deptEntity.toString());
        System.out.println(sysUser.toString());
        return R.ok().put("sysUser",sysUser);
    }

    /**
     * 修改登录用户密码
     */
    @SysLog("修改密码")
    @RequestMapping("/password")
    public R password(String password, String newPassword) {
        Assert.isBlank(newPassword, "新密码不为能空");

        //原密码
        password = ShiroUtils.sha256(password, getUser().getSalt());
        //新密码
        newPassword = ShiroUtils.sha256(newPassword, getUser().getSalt());

        //更新密码
        boolean flag = sysUserService.updatePassword(getUserId(), password, newPassword);
        if (!flag) {
            return R.error("原密码不正确");
        }

        return R.ok();
    }

    /**
     * 用户信息
     */
    @RequestMapping("/info/{userId}")
    @RequiresPermissions("sys:user:info")
    public R info(@PathVariable("userId") Long userId) {
        SysUserEntity user = sysUserService.getById(userId);

        //获取用户所属的角色列表
        List<Long> roleIdList = sysUserRoleService.queryRoleIdList(userId);
        user.setRoleIdList(roleIdList);

        return R.ok().put("user", user);
    }

    /**
     * 保存用户
     */
    @SysLog("保存用户")
    @RequestMapping("/save")
    @RequiresPermissions("sys:user:save")
    public R save(@RequestBody SysUserEntity user) {
        ValidatorUtils.validateEntity(user, AddGroup.class);

        sysUserService.saveUser(user);

        return R.ok();
    }

    /**
     * 修改用户
     */
    @SysLog("修改用户")
    @RequestMapping("/update")
    @RequiresPermissions("sys:user:update")
    public R update(@RequestBody SysUserEntity user) {
        ValidatorUtils.validateEntity(user, UpdateGroup.class);

        sysUserService.update(user);

        return R.ok();
    }

    /**
     * 删除用户
     */
    @SysLog("删除用户")
    @RequestMapping("/delete")
    @RequiresPermissions("sys:user:delete")
    public R delete(@RequestBody Long[] userIds) {
        if (ArrayUtils.contains(userIds, 1L)) {
            return R.error("系统管理员不能删除");
        }

        if (ArrayUtils.contains(userIds, getUserId())) {
            return R.error("当前用户不能删除");
        }

        sysUserService.removeByIds(Arrays.asList(userIds));

        return R.ok();
    }
}
